<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像部分 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" />
            </div>
            <!-- 登录表单 -->
            <!--
              ref属性值：当前表单对象
             -->
            <el-form ref="loginFormRef" label-width="0px" class="login_form" :model="loginForm" :rules="loginFormRules">
                <!-- prop属性设置验证规则 -->
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // loginFormRules是验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮重置表单
    resetLoginForm: function () {
      /*
      this.$refs: {loginFormRef: VueComponent}
      loginFormRef: 表单<el-form>的 ref属性值
      */
      // console.log(this)

      // resetFields(): 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.loginFormRef.resetFields()
    },
    // 点击登录按钮进行登录
    login: function () {
      // 登录预验证
      // validate: 对整个表单进行校验的方法 , 点击登录的时候先调用validate方法验证表单内容是否有误
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.error('用户名或密码有误! 登录失败!')
        // 如果valid参数为true则验证通过, 发起登录请求
        const { data: res } = await this.https.post('/login', this.loginForm)
        // 反馈登录结果
        if (res.meta.status !== 200) return this.$message.error('登录失败' + res.meta.msg)
        this.$message.success('登录成功' + res.meta.msg)
        // console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        // vue路由跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 5px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
    .login_form {
        position: absolute;
        bottom: 0px;
        width: 100%;
        box-sizing: border-box;
        padding: 0 20px;
        .btns {
            display: flex;
            justify-content: end;
        }
    }
}
</style>
